﻿using DevExpress.Web.ASPxTreeView;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.UI.WebControls;

namespace EntryExit
{
    public partial class AttendanceCalendar_orignial : BasePage
    {
        public Dictionary<string, AttendanceInfo> CachedAttendanceData { get; set; }
        public Employee SelectedEmployee { get; set; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                //((MainMaster)this.Master).MasterTreeViewNodeSelected += AttendanceCalendar_MasterTreeViewNodeSelected;
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += AttendanceCalendar_MasterTreeViewNodeSelected;
            }
        }

        void AttendanceCalendar_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            ReInitDataCache();
        }
        private void ReInitDataCache()
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            Session[SessionKeys.CACHED_ATTENDANCE_CALENDAR_DATA] = null;
            Session[SessionKeys.ATTENDANCE_CALENDAR_SHOWING_DATE] = DateTime.MinValue;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTime dt = DateTime.Now.AddMonths(-11);
                //attendanceCalendar.VisibleDate = new DateTime(DateTime.Now.Year, 1, 1);
                attendanceCalendar.VisibleDate = new DateTime(dt.Year, dt.Month, dt.Day);

                ReInitDataCache();
            }
        }

        private void CacheAttendanceData()
        {
            SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            attendanceViewerPanel.HeaderText = "Attendance : " + SelectedEmployee.EmployeeName;
            Session[SessionKeys.ATTENDANCE_CALENDAR_SHOWING_DATE] = attendanceCalendar.VisibleDate;
            Session[SessionKeys.CACHED_ATTENDANCE_CALENDAR_DATA] = EmployeeManagerContext.GetEmployeeAttendanceWithLeave_Original(attendanceCalendar.VisibleDate.AddYears(-1),
                attendanceCalendar.VisibleDate.AddYears(1), SelectedEmployee.CardHolderId, SelectedEmployee.LPN);
        }

        protected void attendanceCalendar_DayRender(object sender, DevExpress.Web.ASPxEditors.DayRenderEventArgs e)
        {
            try
            {
                if ((DateTime)Session[SessionKeys.ATTENDANCE_CALENDAR_SHOWING_DATE] != attendanceCalendar.VisibleDate)
                {
                    CacheAttendanceData();
                }

                AttendanceInfo attendanceInfo = GetAttendanceForDay(e.Day.DateTime);
                if (attendanceInfo != null)
                {
                    switch (attendanceInfo.StatusId)
                    {
                        case UIConstants.IDENTIFIER_NO_DATA_BEFORE_DOJ:
                            e.Style.BackColor = Color.Aqua;
                            e.Day.ToolTip = "No data, Reason : Before DOJ";
                            break;
                        case UIConstants.IDENTIFIER_NO_ATTENDANCE_DATA_NO_LEAVE_DATA:
                            e.Style.BackColor = Color.Red;
                            e.Day.ToolTip = "Unauthorised Absence";
                            break;
                        case UIConstants.IDENTIFIER_DAY_AFTER_TODAY:
                            //Do Nothing
                            break;
                        case UIConstants.IDENTIFIER_NO_ATTENDANCE_DATA_AVAILABLE_IN_FUTURE:
                            //Do Nothing
                            break;
                        case UIConstants.IDENTIFIER_MARKED_AS_LEAVE:
                            e.Style.BackColor = Color.LightBlue;
                            e.Day.ToolTip = "Leave : " + attendanceInfo.LeaveInfo;
                            break;
                        case UIConstants.IDENTIFIER_LOW_WORK_HOURS:
                            e.Style.BackColor = Color.LightPink;
                            e.Day.ToolTip = Convert.ToString(attendanceInfo.TotalInTime);
                            break;

                        case UIConstants.IDENTIFIER_LESSTHAN3_WORK_HOURS:
                            e.Style.BackColor = Color.Orange;
                            e.Day.ToolTip = Convert.ToString(attendanceInfo.TotalInTime);
                            break;

                        case UIConstants.IDENTIFIER_NORMAL_WORK_HOURS:
                            e.Style.BackColor = Color.LightGreen;
                            e.Day.ToolTip = Convert.ToString(attendanceInfo.TotalInTime);
                            break;
                        case UIConstants.IDENTIFIER_OVER_WORK_HOURS:
                            e.Style.BackColor = Color.Yellow;
                            e.Day.ToolTip = Convert.ToString(attendanceInfo.TotalInTime);
                            break;
                        case UIConstants.IDENTIFIER_LOW_WORK_HOURS_HalfDay:
                            e.Style.BackColor = Color.LightPink;
                            e.Day.ToolTip = String.Format("{0} : {1}", Convert.ToString(attendanceInfo.TotalInTime), "Half day leave");
                            break;
                        default:
                            e.Style.BackColor = Color.Turquoise;
                            e.Day.ToolTip = "Unknown Attendance";
                            break;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private AttendanceInfo GetAttendanceForDay(DateTime attendanceDate)
        {
            string dayKey = attendanceDate.ToShortDateString();
            if (Session[SessionKeys.CACHED_ATTENDANCE_CALENDAR_DATA] == null)
            {
                return null;
            }
            else
            {
                if (SelectedEmployee.JoiningDate > attendanceDate)
                {
                    return new AttendanceInfo()
                    {
                        StatusId = UIConstants.IDENTIFIER_NO_DATA_BEFORE_DOJ
                    };
                }
                if (attendanceDate>DateTime.Now)
                {
                    return new AttendanceInfo()
                    {
                        StatusId = UIConstants.IDENTIFIER_DAY_AFTER_TODAY
                    };
                }
                if (CachedAttendanceData == null)
                {
                    CachedAttendanceData = Session[SessionKeys.CACHED_ATTENDANCE_CALENDAR_DATA] as Dictionary<string, AttendanceInfo>;
                }
                if (!CachedAttendanceData.ContainsKey(dayKey))
                {
                    if (attendanceDate < DateTime.Now)
                    {
                        return new AttendanceInfo()
                        {
                            StatusId = UIConstants.IDENTIFIER_NO_ATTENDANCE_DATA_NO_LEAVE_DATA
                        };
                    }
                    else
                    {
                        return new AttendanceInfo()
                        {
                            StatusId=UIConstants.IDENTIFIER_NO_ATTENDANCE_DATA_AVAILABLE_IN_FUTURE
                        };
                    }
                }
                return CachedAttendanceData[attendanceDate.ToShortDateString()];
            }
        }
    }
}